package com.dz.lm.service.goldagent.impl;

import com.alibaba.fastjson.JSON;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.FileLoadUtil;
import com.dz.lm.dao.product.read.AgentProductDao;
import com.dz.lm.dao.user.write.UserDao;
import com.dz.lm.service.AbstractService;
import com.dz.lm.service.goldagent.GoldAgentService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Service
public class GoldAgentServiceImpl extends AbstractService implements GoldAgentService {

	public Logger logger = LogManager.getLogger(getClass());
    
    @Resource(name = "readUserDao")
    private com.dz.lm.dao.user.read.UserDao readUserDao;
    
    @Resource(name = "readAgentDao")
    private com.dz.lm.dao.agent.read.AgentDao readAgentDao;
	
    @Resource(name = "writeUserDao")
    private UserDao userDao;
    
    @Resource(name = "readAgentProductDao")
    private AgentProductDao readAgentProductDao;
    
    public Map<String, Object> qryGoldAgentList(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> retMap = new HashMap<String, Object>();
    	List<Map<String, Object>> agentList = null;
    	Integer count = 0;
		try {
			//获取黄金代理人列表
			agentList = readUserDao.qryGoldAgentList(param);
			//获取黄金代理人条数
			count = readUserDao.qryGoldAgentListCount(param);
		} catch (DaoException e) {
			throw new ServiceException(e.getCause());
		}
		retMap.put("agentList", agentList);
		retMap.put("count", count);
    	return retMap;
    }
    
    public Map<String, Object> qryMyPromoCode(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> userInfo = null;
    	try {
    		userInfo = readUserDao.qryMyPromoCode(param);
		} catch (DaoException e) {
			e.printStackTrace();
		}
    	return userInfo;
    }
    
    public Map<String, Object> qryPerStorInfo(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> perStorInfo = new HashMap<String, Object>();
    	try {
    		perStorInfo = readUserDao.qryPerStorInfo(param);
    		if (perStorInfo != null && !perStorInfo.isEmpty() && "1".equals(perStorInfo.get("perOpenStoreFlag"))) {
        		perStorInfo.put("perStoreUrl", FileLoadUtil.getPerStoreUrl() + param.get("userId"));
    		} else {
        		perStorInfo.put("perStoreUrl", "");
    		}
		} catch (DaoException e) {
			e.printStackTrace();
		}
    	return perStorInfo;
    }
    
    public Map<String, Object> applyGoldAgent(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> retData = new HashMap<String, Object>();
    	try {
    		//等于0默认满足条件
    		String result = "0";
    		//查询下级代理是否大于20人
    		int cnt = readAgentDao.qry2LevelAgentByUserIdCount(param);
    		if (cnt < 20) {
    			//代理人数不满足条件
    			result = "1";
    			//查询是否有推荐的机构
    			cnt = readUserDao.qryRecommendMoneyBarByUserIdCount(param);
    			if (cnt <= 0) {
    				//拓展的机构数不满足条件
    				result = "2";
    			} else {
    				result = "0";
        		}
    		}
    		if ("0".equals(result)) {
        		//查询当前代理人是否存在二级代理
        		cnt = readAgentDao.qryIs2LevelAgentByUserIdCount(param);
        		if (cnt > 0) {
        			result = "3";
        		} else {
        			param.put("goldAgentFlag", "1");
        			param.put("perOpenStoreFlag", "1");
        			param.put("maxAgentCount", 200);
        			userDao.updateManagerInfo(param);
        		}
    		}
    		retData.put("result", result);
		} catch (DaoException e) {
			e.printStackTrace();
		}
    	return retData;
    }
    
    public Map<String, Object> applyPerStore(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> retData = new HashMap<String, Object>();
    	try {
    		logger.info(JSON.toJSON(param)+"==================二维码参数");
    		param.put("perOpenStoreFlag", "1");
    		int cnt = userDao.updateManagerInfo(param);
    		if (cnt == 1) {
        		retData.put("perStoreUrl", FileLoadUtil.getPerStoreUrl() + param.get("userId"));
    		} else {
        		retData.put("perStoreUrl", null);
    		}
		} catch (DaoException e) {
			e.printStackTrace();
		}
    	return retData;
    }
    
    public Map<String, Object> qryMyAllProductApplyList(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> retMap = new HashMap<String, Object>();
    	List<Map<String, Object>> applyList = null;
    	Map<String, Object> totalMap = null;
    	Integer count = 0;
		try {
			//查询我创建的和代理的产品的申请信息列表
			applyList = readAgentProductDao.qryMyAllProductApplyList(param);
			//查询我创建的和代理的产品的申请信息条数
			count = readAgentProductDao.qryMyAllProductApplyListCount(param);
			//统计历史申请数及累计收入
			totalMap = readAgentProductDao.qryMyAllProductApplyTotal(param);
		} catch (DaoException e) {
			throw new ServiceException(e.getCause());
		}
		retMap.put("applyList", applyList);
		retMap.put("count", count);
		retMap.put("historyApplyCount", count);
		retMap.put("totalPayAmount", totalMap.get("totalPayAmount"));
    	return retMap;
    }
    
    public Map<String, Object> qryAgentList(Map<String, Object> param) throws ServiceException {
    	Map<String, Object> retMap = new HashMap<String, Object>();
    	List<Map<String, Object>> agentList = null;
    	Integer count = 0;
		try {
			//获取代理人列表
			agentList = readUserDao.qryAgentList(param);
			//获取代理人条数
			count = readUserDao.qryAgentListCount(param);
		} catch (DaoException e) {
			throw new ServiceException(e.getCause());
		}
		retMap.put("agentList", agentList);
		retMap.put("count", count);
    	return retMap;
    }
}
